(function(){

var BusCon = require("../../core/bus/_con");
var db = require("../../core/db/index");
var config = require("../../core/_include/config");
var ResponObj = require("../_include/ResponObj");
var myfs = require("../_include/fs");
var theme = require("./theme");
var role = require("./role");
module.exports = {
	prefix:"/bus",
	"post":{
		//注册一个商家
		//并为此商家建立一个独立的站点文件夹
		/*
		 * body:{bus.info}
		 * query:{
		 *     theme:"theme_name"
		 * }
		 }
		 */
		"/register":function *(next){
			var bus_info = this.request.body;
			var bus_name = bus_info.name.toLowerCase();
			if (db.find_one(config.dbname.bus,{
				info:{
					name:bus_name
				}
			})) {
				//商家名就是ID
				this.body = ResponObj("error","商家名已经存在");
			}else if(!RegExp("^([\u4E00-\uFA29]|[\uE7C7-\uE7F3]|[a-zA-Z0-9])*$").test(bus_info.name)){
				this.body = ResponObj("error","商家名格式错误");
			}else if(!(bus_info.password&&bus_info.confirm_password)){
				this.body = ResponObj("error","表单不完整");
			}else if(bus_info.password !== bus_info.confirm_password){
				this.body = ResponObj("error","两次密码输入不匹配");
			}else{
				delete bus_info.confirm_password;
				//账号注册到数据库中
				new BusCon(bus_info)
				//初始化商家独立文件夹
				myfs.mkdir("_bus_instance/"+bus_name,0755,function(){
					console.log("文件夹“",bus_name,"”创建完成");
				});
				//初始化配置文件
				myfs.file("_bus_instance/"+bus_name+"/config.json",JSON.stringify(bus_info));
				this.body = theme.initBusTheme(bus_name,this.request.query.theme);
			}
			yield next;
		},
		"/login":function *(next){
			var form_data = this.request.body;
			try{
				var bus = BusCon.login(form_data);
				this.body = ResponObj("json","登陆成功");
				//删除loginHash中同用户名的信息
				db.remove_list(config.dbname.loginHash,{
					bus_id:bus._id
				});
				//注入新的Session
				var hash = bus._id.substr(2)+(+new Date).toString(16);
				this.cookies.set("loginSession",hash);
				db.insert(config.dbname.loginHash,{
					_id:hash,
					bus_id:bus._id,
					type:"bus",
					beginTime:+new Date
				});
			}catch(errorMsg){
				this.body = ResponObj("error",errorMsg);
			}
			yield next;
		}
	},
	"get":{
		"/:name":function *(next){
			//商家名不分大小写，同文件夹
			var name = this.params.name.toLowerCase();
			var result;
			// if (name.indexOf(config.dbIdPrefix.bus)===0) {
			result = db.find_by_id(config.dbname.bus,name);
			// }
			if (result) {
				this.body = ResponObj("json",result);
			}else{
				this.body = ResponObj("error","找不到指定商家");
			}
			yield next;
		},
		//获取当前登录的商家信息
		"/":function *(next){
			try{
				var bus  = role.getBus(this);
				this.body = bus;
			}catch(errorMsg){
				this.body = ResponObj("error",errorMsg);
			}
			yield next;
		},
		//商家退出登录
		"/loginout":function *(){
			try{
				var bus  = role.getBus(this);
				//删除loginHash中同商家名的信息
				db.remove_list(config.dbname.loginHash,{
					bus_id:bus._id
				});
				//清空Cookie
				this.cookies.set("loginSession","");
				this.body = ResponObj("json","商家下线成功");
			}catch(errorMsg){
				this.body = ResponObj("error",errorMsg);
			}
		}
	},
	"delete":{
		"/:id":function *(next){
			this.body = "bug del "+this.params.id;
			yield next;
		}
	}
}

}());
